from app import db
from datetime import datetime

class StyleTemplate(db.Model):
    """风格模板表"""
    __tablename__ = 'style_templates'
    
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False, comment='模板名称')
    description = db.Column(db.Text, nullable=True, comment='模板描述')
    category = db.Column(db.String(50), nullable=False, comment='分类: portrait, art, fun, professional等')
    
    # 提示词模板
    positive_prompt = db.Column(db.Text, nullable=False, comment='正向提示词模板')
    negative_prompt = db.Column(db.Text, nullable=False, comment='负向提示词模板')
    
    # 预览图
    preview_image_url = db.Column(db.String(500), nullable=True, comment='预览图URL')
    thumbnail_url = db.Column(db.String(500), nullable=True, comment='缩略图URL')
    
    # 参数配置
    recommended_width = db.Column(db.Integer, default=512, comment='推荐宽度')
    recommended_height = db.Column(db.Integer, default=512, comment='推荐高度')
    recommended_steps = db.Column(db.Integer, default=20, comment='推荐步数')
    recommended_cfg_scale = db.Column(db.Float, default=7.0, comment='推荐CFG Scale')
    
    # 显示设置
    sort_order = db.Column(db.Integer, default=0, comment='排序权重')
    is_active = db.Column(db.Boolean, default=True, comment='是否启用')
    is_premium = db.Column(db.Boolean, default=False, comment='是否为VIP专属')
    
    # 统计信息
    usage_count = db.Column(db.Integer, default=0, comment='使用次数')
    
    # 时间戳
    created_at = db.Column(db.DateTime, default=datetime.utcnow, comment='创建时间')
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment='更新时间')
    
    def __repr__(self):
        return f'<StyleTemplate {self.name}>'
    
    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'description': self.description,
            'category': self.category,
            'positive_prompt': self.positive_prompt,
            'negative_prompt': self.negative_prompt,
            'preview_image_url': self.preview_image_url,
            'thumbnail_url': self.thumbnail_url,
            'recommended_width': self.recommended_width,
            'recommended_height': self.recommended_height,
            'recommended_steps': self.recommended_steps,
            'recommended_cfg_scale': self.recommended_cfg_scale,
            'is_premium': self.is_premium,
            'usage_count': self.usage_count,
            'created_at': self.created_at.isoformat() if self.created_at else None
        }
    
    def increment_usage(self):
        """增加使用次数"""
        self.usage_count += 1
        db.session.commit()